package com.itheima.tilas.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.tilas.Utils.ResultUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 132
 */
@RestController
@RequestMapping("/cookie")
public class TestCookieController {
    @Resource
    Environment environment;
    @GetMapping("/save")
    public ResultUtil saveCookie(HttpServletResponse response) {
        Cookie cookie = new Cookie("username", "Tom");
        response.addCookie(cookie);
        Cookie cookie1 = new Cookie("password", "123456");
        response.addCookie(cookie1);
        return ResultUtil.success();
    }

    @GetMapping("/get")
    public ResultUtil saveCookie(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            jsonObject.put(cookie.getName(), cookie.getValue());
        }
        return ResultUtil.success(jsonObject);
    }

    @GetMapping
    public ResultUtil Cookie() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("endpoint",environment.getProperty("aliyun.oss.endpoint"));
        return ResultUtil.success(jsonObject);
    }
}
